package com.ruoyi.web.controller.system;

import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AdminUpdownBranch;
import com.ruoyi.system.domain.AdminUserRechargeRecord;
import com.ruoyi.system.service.IAdminUpdownBranchService;
import com.ruoyi.system.service.IAdminUserRechargeRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AdminWeixinUser;
import com.ruoyi.system.service.IAdminWeixinUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 微信用户信息Controller
 *
 * @author ruoyi
 * @date 2020-11-06
 */
@RestController
@RequestMapping("/system/wechatUser")
public class AdminWeixinUserController extends BaseController
{
    @Autowired
    private IAdminWeixinUserService adminWeixinUserService;

    @Autowired
    private IAdminUserRechargeRecordService adminUserRechargeRecordService;

    @Autowired
    private IAdminUpdownBranchService iAdminUpdownBranchService;
    /**
     * 查询微信用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:wechatUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminWeixinUser adminWeixinUser)
    {
        startPage();
        String room = SecurityUtils.getLoginUser().getUser().getRoom();
        adminWeixinUser.setRoom(room);
        List<AdminWeixinUser> list = adminWeixinUserService.selectAdminWeixinUserList(adminWeixinUser);
        return getDataTable(list);
    }

    /**
     * 导出微信用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:wechatUser:export')")
    @Log(title = "微信用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AdminWeixinUser adminWeixinUser)
    {
        List<AdminWeixinUser> list = adminWeixinUserService.selectAdminWeixinUserList(adminWeixinUser);
        ExcelUtil<AdminWeixinUser> util = new ExcelUtil<AdminWeixinUser>(AdminWeixinUser.class);
        return util.exportExcel(list, "wechatUser");
    }

    /**
     * 获取微信用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:wechatUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(adminWeixinUserService.selectAdminWeixinUserById(id));
    }

    /**
     * 新增微信用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:wechatUser:add')")
    @Log(title = "微信用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminWeixinUser adminWeixinUser)
    {
        return toAjax(adminWeixinUserService.insertAdminWeixinUser(adminWeixinUser));
    }

    /**
     * 修改微信用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:wechatUser:edit')")
    @Log(title = "微信用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminWeixinUser adminWeixinUser)
    {
        return toAjax(adminWeixinUserService.updateAdminWeixinUser(adminWeixinUser));
    }

    /**
     * 删除微信用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:wechatUser:remove')")
    @Log(title = "微信用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adminWeixinUserService.deleteAdminWeixinUserByIds(ids));
    }

    /**
     * 充值
     */
    @PreAuthorize("@ss.hasPermi('system:wechatUser:recharge')")
    @Log(title = "微信用户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/recharge")
    public AjaxResult recharge(@RequestBody AdminWeixinUser user)
    {
        String room = SecurityUtils.getLoginUser().getUser().getRoom();
        AdminWeixinUser adminWeixinUser = adminWeixinUserService.selectAdminWeixinUserById(user.getId());
        if(adminWeixinUser != null){
            if (user.getFlag()==1) {
                AdminWeixinUser updateUser = new AdminWeixinUser();
                updateUser.setId(user.getId());
                updateUser.setPrice(adminWeixinUser.getPrice().add(user.getPrice()));
                int count = adminWeixinUserService.updateAdminWeixinUser(updateUser);
                if(count>0){
                    AdminUpdownBranch adminUpdownBranch = new AdminUpdownBranch();
                    adminUpdownBranch.setUserId(adminWeixinUser.getId());
                    adminUpdownBranch.setCreateTime(DateUtils.getNowDate());
                    adminUpdownBranch.setWexinId(adminWeixinUser.getWexinId());
                    adminUpdownBranch.setWexinName(adminWeixinUser.getWexinName());
                    adminUpdownBranch.setRoom(room);
                    adminUpdownBranch.setBranchType(1);
                    adminUpdownBranch.setAuditStatus(1);
                    adminUpdownBranch.setPrice(user.getPrice());
                    adminUpdownBranch.setCreateTime(DateUtils.getNowDate());
                    iAdminUpdownBranchService.insertAdminUpdownBranch(adminUpdownBranch);
                    AdminUserRechargeRecord adminUserRechargeRecord = new AdminUserRechargeRecord();
                    adminUserRechargeRecord.setUserId(user.getId());
                    adminUserRechargeRecord.setWexinId(adminWeixinUser.getWexinId());
                    adminUserRechargeRecord.setWexinName(adminWeixinUser.getWexinName());
                    adminUserRechargeRecord.setRechargePrice(user.getPrice());
                    adminUserRechargeRecord.setCreateTime(new Date());
                    adminUserRechargeRecord.setRoom(room);
                    return toAjax(adminUserRechargeRecordService.insertAdminUserRechargeRecord(adminUserRechargeRecord));
                }
                return toAjax(count);
            }else if(user.getFlag()==2){
                if(adminWeixinUser.getPrice().setScale(2, RoundingMode.HALF_UP).compareTo(user.getPrice().setScale(2, RoundingMode.HALF_UP))>=0){
                    AdminWeixinUser updateUser = new AdminWeixinUser();
                    updateUser.setId(user.getId());
                    updateUser.setPrice(adminWeixinUser.getPrice().subtract(user.getPrice()));
                    int count = adminWeixinUserService.updateAdminWeixinUser(updateUser);
                    if(count>0){
                        AdminUpdownBranch adminUpdownBranch = new AdminUpdownBranch();
                        adminUpdownBranch.setUserId(adminWeixinUser.getId());
                        adminUpdownBranch.setCreateTime(DateUtils.getNowDate());
                        adminUpdownBranch.setWexinId(adminWeixinUser.getWexinId());
                        adminUpdownBranch.setWexinName(adminWeixinUser.getWexinName());
                        adminUpdownBranch.setRoom(room);
                        adminUpdownBranch.setBranchType(2);
                        adminUpdownBranch.setAuditStatus(1);
                        adminUpdownBranch.setPrice(user.getPrice());
                        adminUpdownBranch.setCreateTime(DateUtils.getNowDate());
                        iAdminUpdownBranchService.insertAdminUpdownBranch(adminUpdownBranch);
                        AdminUserRechargeRecord adminUserRechargeRecord = new AdminUserRechargeRecord();
                        adminUserRechargeRecord.setUserId(user.getId());
                        adminUserRechargeRecord.setWexinId(adminWeixinUser.getWexinId());
                        adminUserRechargeRecord.setWexinName(adminWeixinUser.getWexinName());
                        adminUserRechargeRecord.setRechargePrice(user.getPrice().negate());
                        adminUserRechargeRecord.setCreateTime(new Date());
                        adminUserRechargeRecord.setRoom(room);
                        return toAjax(adminUserRechargeRecordService.insertAdminUserRechargeRecord(adminUserRechargeRecord));
                    }
                    return toAjax(count);
                }else {
                    return AjaxResult.error();
                }

            }else{
                return AjaxResult.error("操作有误");
            }
        }
        return AjaxResult.error();
    }

    @Log(title = "修改用户关闭状态", businessType = BusinessType.UPDATE)
    @GetMapping("/handleUpdateFlag")
    public AjaxResult handleUpdateFlag(AdminWeixinUser user)
    {
        return toAjax(adminWeixinUserService.updateAdminWeixinUser(user));
    }

}
